using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SqlAdmin;

namespace SqlWebAdmin
{
    public partial class DeleteDatabase : System.Web.UI.Page
    {


        public DeleteDatabase()
        {
            Page.Init += new System.EventHandler(Page_Init);
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["WebDataAdministrator"];
            if (cookie == null)
                Response.Redirect("default.aspx?error=sessionexpired");

            SqlServer server = new SqlServer(cookie.Values["server"], cookie.Values["username"], cookie.Values["password"]);

            DatabaseNameLabel.Text = Server.HtmlEncode(Request["database"]);
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            InitializeComponent();
        }

        #region Web Form Designer generated code
        private void InitializeComponent()
        {    

        }
        #endregion

        protected void YesButton_Click(object sender, System.EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["WebDataAdministrator"];
            if (cookie == null)
                Response.Redirect("default.aspx?error=sessionexpired");

            SqlServer server = new SqlServer(cookie.Values["server"], cookie.Values["username"], cookie.Values["password"]);

            server.Connect();

            SqlDatabase database = server.Databases[Request["database"]];

            if (database != null) {
                database.Remove();
            }
            else {
                server.Disconnect();

                Response.Redirect(String.Format("error.aspx?error={1}", 1000));
                return;
            }

            server.Disconnect();

            Response.Redirect("databases.aspx");
        }

        protected void NoButton_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("tables.aspx?database=" + Server.UrlEncode(Request["database"]));
        }
    }
}
